/*
    * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
 */

#include "file_share_stub.h"
#include "jsonUtil.hpp"

namespace OHOS {
namespace FileShare {
FileShareStub::FileShareStub()
{
    LOGD("[FileShareStub::FileShareStub] start.");

    baseFuncs_[SEND_FILE] = &FileShareStub::SendFileStub;
    baseFuncs_[REGISTER_FILE_SHARE_CALLBACK] = &FileShareStub::RegisterFileShareCallbackStub;
    baseFuncs_[UNREGISTER_FILE_SHARE_CALLBACK] = &FileShareStub::UnRegisterFileShareCallbackStub;
    baseFuncs_[CANCEL_SEND_FILE] = &FileShareStub::CancelSendFileStub;
    baseFuncs_[SET_SHARE_STATUS] = &FileShareStub::SetShareStatusStub;
    baseFuncs_[GET_SHARE_STATUS] = &FileShareStub::GetShareStatusStub;
    baseFuncs_[GET_STORAGE_PATH] = &FileShareStub::GetStoragePathStub;
    baseFuncs_[SET_STORAGE_PATH] = &FileShareStub::SetStoragePathStub;
    baseFuncs_[GET_WORK_TIME] = &FileShareStub::GetWorkDurationStub;
    baseFuncs_[SET_WORK_TIME] = &FileShareStub::SetWorkDurationStub;
    baseFuncs_[REGISTER_SHARE_STATUS_CALLBACK] = &FileShareStub::RegisterShareStatusCallbackStub;
    baseFuncs_[UNREGISTER_SHARE_STATUS_CALLBACK] = &FileShareStub::UnRegisterShareStatusCallbackStub;
    baseFuncs_[CHANGE_UUID] = &FileShareStub::ChangeUuidStub;
}

FileShareStub::~FileShareStub()
{
    LOGD("[FileShareStub::~FileShareStub] start.");

    baseFuncs_.clear();
}

int32_t FileShareStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option)
{
    LOGD("OnRemoteRequest start, cmd: %{public}u", code);
    auto itFunc = baseFuncs_.find(code);
    if (itFunc != baseFuncs_.end())
    {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr)
        {
            return (this->*memberFunc)(data, reply);
        }
    }
    LOGD("remoterequest no member function default process");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int32_t FileShareStub::SendFileStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("SendFileStub start.");

    std::string taskStr = data.ReadString();
    nlohmann::json jsonObject = nlohmann::json::parse(taskStr);
    if (jsonObject.is_discarded()) {
        LOGE("SendFileStub parse error");
        return FS_INVALID_PARAM;
    }
    
    SendTask task;
    JsonToSendTask(jsonObject, task);

    auto ret = SendFile(task);
    if (ret != FS_SUCCESS){
        LOGE("SendFile failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::RegisterFileShareCallbackStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("RegisterFileShareCallbackStub start.");
    sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
    sptr<IFileShareCallback> callback = iface_cast<IFileShareCallback>(remoteObject);
    int32_t ret = RegisterFileShareCallback(callback);
    if (ret != FS_SUCCESS){
        LOGE("RegisterFileShareCallback failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::UnRegisterFileShareCallbackStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("UnRegisterFileShareCallbackStub start.");
    int32_t ret = UnRegisterFileShareCallback();
    if (ret != FS_SUCCESS){
        LOGE("UnRegisterFileShareCallback failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::CancelSendFileStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("CancelSendFileStub start.");
    std::string deviceId = data.ReadString();
    SendType type = (SendType) data.ReadInt32();

    int32_t ret = CancelSendFile(deviceId, type);
    if (ret != FS_SUCCESS){
        LOGE("CancelSendFile failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::SetShareStatusStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("SetShareStatusStub start.");
    bool status = data.ReadBool();

    int32_t ret = SetShareStatus(status);
    if (ret != FS_SUCCESS){
        LOGE("SetShareStatus failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS;
}

int32_t FileShareStub::GetShareStatusStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("GetShareStatusStub start.");
    bool status;
    int32_t ret = GetShareStatus(status);
    if (ret != FS_SUCCESS){
        LOGE("GetShareStatus failed.");
        reply.WriteBool(false);
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteBool(status);
    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS;
}

int32_t FileShareStub::GetStoragePathStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("GetStoragePathStub start.");
    std::string path = GetStoragePath();
    if (path == ""){
        LOGE("GetStoragePath failed.");  
        return FS_GET_STORAGE_PATH_ERROR;
    }
    reply.WriteString(path);   
    return FS_SUCCESS; 
}

int32_t FileShareStub::SetStoragePathStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("SetStoragePathStub start.");
    std::string path = data.ReadString();
    int32_t ret = SetStoragePath(path);
    if (ret != FS_SUCCESS){
        LOGE("SetStoragePath failed.");  
        return ret;
    }
    return FS_SUCCESS; 
}

int32_t FileShareStub::GetWorkDurationStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("GetWorkDurationStub start.");
    int32_t time = GetWorkDuration();
    if (time < 0){
        LOGE("GetWorkDuration failed.");  
        return FS_GET_WORK_DURATION_ERROR;
    }
    reply.WriteInt32(time);
    return FS_SUCCESS; 
}

int32_t FileShareStub::SetWorkDurationStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("SetWorkDurationStub start.");
    int32_t time = data.ReadInt32();
    int32_t ret = SetWorkDuration(time);
    if (ret != FS_SUCCESS){
        LOGE("SetWorkDurationStub failed.");  
        return ret;
    }
    return FS_SUCCESS; 
}

int32_t FileShareStub::RegisterShareStatusCallbackStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("RegisterShareStatusCallbackStub start.");

    std::string bundleName = data.ReadString();
    sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
    sptr<IFileShareCallback> callback = iface_cast<IFileShareCallback>(remoteObject);
    int32_t ret = RegisterShareStatusCallback(bundleName, callback);
    if (ret != FS_SUCCESS){
        LOGE("RegisterShareStatusCallback failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::UnRegisterShareStatusCallbackStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("UnRegisterShareStatusCallbackStub start.");

    std::string bundleName = data.ReadString();
    int32_t ret = UnRegisterShareStatusCallback(bundleName);
    if (ret != FS_SUCCESS){
        LOGE("UnRegisterShareStatusCallback failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}

int32_t FileShareStub::ChangeUuidStub(MessageParcel &data, MessageParcel &reply)
{
    LOGD("ChangeUuidStub start.");

    int32_t uuidInt32 = data.ReadInt32();
    uint16_t uuid = static_cast<uint16_t>(uuidInt32);
    int32_t ret = ChangeUuid(uuid);
    if (ret != FS_SUCCESS){
        LOGE("ChangeUuid failed.");  
        reply.WriteInt32(ret);   
        return ret;
    }

    reply.WriteInt32(FS_SUCCESS);
    return FS_SUCCESS; 
}
} // namespace FileShare
} // namespace OHOS